package com.ofhb.webapp.client.forms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.ofhb.webapp.client.utils.AppClientUtils;
import com.ofhb.webapp.client.utils.DataCache;
import com.ofhb.webapp.client.utils.RegEx;
import com.ofhb.webapp.client.utils.ServiceUtil;
import com.ofhb.webapp.shared.dto.CityDTO;
import com.ofhb.webapp.shared.dto.HallDTO;
import com.ofhb.webapp.shared.dto.UserDTO;

public class HallForm extends FlexTable {

	private Map<String, UserDTO> mapUserDTO = new HashMap<String, UserDTO>();
	
	// Name Information
	private ListBox comboBoxOwnerOfHall;
	private TextBox textBoxHallFullName;
//	private TextBox textBoxOwnerOfHall;
	private TextBox textBoxShortName;

	// Demographic Information
	private TextBox textBoxAddressLine1;
	private TextBox textBoxAddressLine2;
	private SuggestBox suggestBoxCityState;
	private TextBox textBoxPinCode;
	private TextBox textBoxLandmark;
	private TextBox textBoxContactNumber1;
	private TextBox textBoxContactNumber2;
	private TextBox textBoxContactNumber3;
	private TextBox textBoxPrimaryEmail;

	// Facility Information
	private ListBox listBoxHallType;
	private CheckBox chckbxAirConditionerFacility;
	private CheckBox chckbxGeneratorFacility;
	private TextBox textBoxFunctionalHallCapacity;
	private FlexTable ftErrorHallType;
	private TextBox textBoxGeneralRoomsAC;
	private TextBox textBoxFourWheelersOutside;
	private TextArea textAreaDesc;
	private TextBox textBoxFourWheelersInside;
	private TextBox textBoxBrideRoomsNonAC;
	private TextBox textBoxGroomRoomsNonAC;
	private TextBox textBoxGeneralRoomsNonAC;
	private TextBox textBoxTwoWheelersInside;
	private TextBox textBoxBrideRoomsAC;
	private TextBox textBoxDiningHallCapacity;
	private TextBox textBoxTwoWheelersOutside;
	private TextBox textBoxGroomRoomsAC;
	private FlexTable ftFormFacilityInformation;

	private ArrayList<FocusWidget> formValidationWidgets;
	
	
	private FlexTable ftErrorOwnerOfHall;
	
	private Button btnSubmit;
	private Button btnReset;
	

	public HallForm() {

		setSize("100%", "");

		VerticalPanel vpNameInformation = new VerticalPanel();
		vpNameInformation.setStyleName("ofhb-form-section");
		setWidget(0, 0, vpNameInformation);
		vpNameInformation.setWidth("100%");

		Label lblNameInformation = new Label("Name Information");
		vpNameInformation.add(lblNameInformation);
		lblNameInformation.setStyleName("ofhb-form-section-header-lbl");

		FlexTable ftFormNameInformation = new FlexTable();
		ftFormNameInformation.setCellSpacing(5);
		vpNameInformation.add(ftFormNameInformation);

		Label lblOwnerOf = new Label("* Owner of Hall :");
		lblOwnerOf.setStyleName("ofhb-form-field-required-lbl");
		ftFormNameInformation.setWidget(0, 0, lblOwnerOf);
		lblOwnerOf.setWidth("175px");

		FlexTable flexTableOwnerOfHall = new FlexTable();
		ftFormNameInformation.setWidget(0, 1, flexTableOwnerOfHall);
		
		//		textBoxOwnerOfHall = new TextBox();
		//		textBoxOwnerOfHall.setVisibleLength(32);
		//		textBoxOwnerOfHall.setMaxLength(64);
		//		flexTableOwnerOfHall.setWidget(0, 0, textBoxOwnerOfHall);
				
		ftErrorOwnerOfHall = new FlexTable();
		ftErrorOwnerOfHall.setStyleName("ofhb-form-field-error-table");
		flexTableOwnerOfHall.setWidget(0, 1, ftErrorOwnerOfHall);
		ftErrorOwnerOfHall.setVisible(false);

		Image imageError = new Image("resources/images/field_error.png");
		ftErrorOwnerOfHall.setWidget(0, 0, imageError);
		imageError.setSize("16px", "16px");

		Label lblErrorOwnerOfHall = new Label("Please select Owner of the hall");
		lblErrorOwnerOfHall.setWordWrap(false);
		ftErrorOwnerOfHall.setWidget(0, 1, lblErrorOwnerOfHall);

		
		
		comboBoxOwnerOfHall = new ListBox();
		flexTableOwnerOfHall.setWidget(0, 0, comboBoxOwnerOfHall);
		comboBoxOwnerOfHall.getElement().setPropertyObject("errorBox", ftErrorOwnerOfHall);
		
		AsyncCallback<List<UserDTO>> callback = new AsyncCallback<List<UserDTO>>() {
			
			@Override
			public void onSuccess(List<UserDTO> result) {
				if(result != null && result.size() > 0) {
					mapUserDTO.clear();
					comboBoxOwnerOfHall.clear();
					comboBoxOwnerOfHall.addItem("-Select Owner of Hall-");
					for (UserDTO userDTO : result) {
						comboBoxOwnerOfHall.addItem(userDTO.getFirstName(), String.valueOf(userDTO.getId()));
						mapUserDTO.put( String.valueOf(userDTO.getId()), userDTO);
					}
				}
			}
			
			@Override
			public void onFailure(Throwable arg0) {
				
			}
		};
		ServiceUtil.DATA_READER_SERVICE.getHallOwners(callback);
		

		Label lblHallFullName = new Label("* Hall Full Name :");
		lblHallFullName.setStyleName("ofhb-form-field-required-lbl");
		ftFormNameInformation.setWidget(1, 0, lblHallFullName);
		lblHallFullName.setWidth("175px");

		FlexTable ftHallFullName = new FlexTable();
		ftFormNameInformation.setWidget(1, 1, ftHallFullName);

		textBoxHallFullName = new TextBox();
		textBoxHallFullName.setMaxLength(64);
		ftHallFullName.setWidget(0, 0, textBoxHallFullName);
		textBoxHallFullName.setVisibleLength(32);

		textBoxHallFullName.getElement().setAttribute("required", "true");
		textBoxHallFullName.getElement().setAttribute("regEx", RegEx.NAME);
		textBoxHallFullName.getElement().setPropertyObject("errorBox",
				AppClientUtils.getFieldErrorBox("Please enter valid Official Hall Name"));

		Label lblShortName = new Label("* Short Name :");
		lblShortName.setStyleName("ofhb-form-field-required-lbl");
		ftFormNameInformation.setWidget(2, 0, lblShortName);
		lblShortName.setWidth("175px");

		FlexTable ftShortName = new FlexTable();
		ftFormNameInformation.setWidget(2, 1, ftShortName);

		textBoxShortName = new TextBox();
		textBoxShortName.setMaxLength(64);
		textBoxShortName.setVisibleLength(32);
		ftShortName.setWidget(0, 0, textBoxShortName);

		textBoxShortName.getElement().setAttribute("required", "true");
		textBoxShortName.getElement().setAttribute("regEx", RegEx.NAME);
		textBoxShortName.getElement().setPropertyObject("errorBox",
				AppClientUtils.getFieldErrorBox("Please enter valid Short Name for display purpose"));

		ftFormNameInformation.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		ftFormNameInformation.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		ftFormNameInformation.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);

		VerticalPanel vpDemographicInformation = new VerticalPanel();
		vpDemographicInformation.setStyleName("ofhb-form-section");
		setWidget(2, 0, vpDemographicInformation);
		vpDemographicInformation.setWidth("100%");

		Label lblDemographicInformation = new Label("Demographic Information");
		lblDemographicInformation.setStyleName("ofhb-form-section-header-lbl");
		vpDemographicInformation.add(lblDemographicInformation);

		FlexTable ftFormDemographicInformation = new FlexTable();
		ftFormDemographicInformation.setCellSpacing(5);
		vpDemographicInformation.add(ftFormDemographicInformation);
		ftFormDemographicInformation.setSize("", "");

		Label lblAddressLine1 = new Label("* Address Line 1 :");
		lblAddressLine1.setStyleName("ofhb-form-field-required-lbl");
		ftFormDemographicInformation.setWidget(0, 0, lblAddressLine1);
		lblAddressLine1.setWidth("175px");

		FlexTable ftAddressLine1 = new FlexTable();
		ftFormDemographicInformation.setWidget(0, 1, ftAddressLine1);

		textBoxAddressLine1 = new TextBox();
		textBoxAddressLine1.setMaxLength(128);
		textBoxAddressLine1.setVisibleLength(50);
		ftAddressLine1.setWidget(0, 0, textBoxAddressLine1);

		textBoxAddressLine1.getElement().setAttribute("required", "true");
		textBoxAddressLine1.getElement().setAttribute("regEx", RegEx.ADDRESS);
		textBoxAddressLine1.getElement().setPropertyObject("errorBox",
				AppClientUtils.getFieldErrorBox("Please enter valid Address Line 1 (No Special Charaters)"));

		Label lblAddressLine2 = new Label("Address Line 2 :");
		lblAddressLine2.setStyleName("ofhb-form-field-lbl");
		ftFormDemographicInformation.setWidget(1, 0, lblAddressLine2);
		lblAddressLine2.setWidth("175px");

		FlexTable ftAddressLine2 = new FlexTable();
		ftFormDemographicInformation.setWidget(1, 1, ftAddressLine2);

		textBoxAddressLine2 = new TextBox();
		textBoxAddressLine2.setMaxLength(128);
		textBoxAddressLine2.setVisibleLength(50);
		ftAddressLine2.setWidget(0, 0, textBoxAddressLine2);

		textBoxAddressLine2.getElement().setAttribute("required", "false");
		textBoxAddressLine2.getElement().setAttribute("regEx", RegEx.ADDRESS);
		textBoxAddressLine2.getElement().setPropertyObject("errorBox",
				AppClientUtils.getFieldErrorBox("Please enter valid Address Line 2 (No Special Charaters)"));

		Label lblCity = new Label("* City :");
		lblCity.setStyleName("ofhb-form-field-required-lbl");
		ftFormDemographicInformation.setWidget(2, 0, lblCity);
		lblCity.setWidth("175px");

		suggestBoxCityState = new SuggestBox(DataCache.getCityOracle());
		suggestBoxCityState.setWidth("200px");

		FlexTable ftCity = new FlexTable();
		ftFormDemographicInformation.setWidget(2, 1, ftCity);

		suggestBoxCityState.getElement().setAttribute("required", "true");
		suggestBoxCityState.getElement().setPropertyObject("errorBox",
				AppClientUtils.getFieldErrorBox("Please enter valid City (No Special Charaters)"));
		ftCity.setWidget(0, 0, suggestBoxCityState);

		Label lblPinCode = new Label("* Pin Code :");
		lblPinCode.setStyleName("ofhb-form-field-required-lbl");
		ftFormDemographicInformation.setWidget(3, 0, lblPinCode);
		lblPinCode.setWidth("175px");

		FlexTable ftPinCode = new FlexTable();
		ftFormDemographicInformation.setWidget(3, 1, ftPinCode);

		textBoxPinCode = new TextBox();
		textBoxPinCode.setMaxLength(6);
		textBoxPinCode.setVisibleLength(6);
		ftPinCode.setWidget(0, 0, textBoxPinCode);

		textBoxPinCode.getElement().setAttribute("required", "true");
		textBoxPinCode.getElement().setAttribute("regEx", RegEx.PIN_CODE);
		textBoxPinCode.getElement().setPropertyObject("errorBox",
				AppClientUtils.getFieldErrorBox("Please enter 6 digit pin code number"));

		Label lblLandmark = new Label("* Landmark");
		lblLandmark.setStyleName("ofhb-form-field-required-lbl");
		ftFormDemographicInformation.setWidget(4, 0, lblLandmark);
		lblLandmark.setWidth("175px");

		FlexTable flexTableLandmark = new FlexTable();
		ftFormDemographicInformation.setWidget(4, 1, flexTableLandmark);

		textBoxLandmark = new TextBox();
		textBoxLandmark.setName("128");
		textBoxLandmark.setVisibleLength(30);
		flexTableLandmark.setWidget(0, 0, textBoxLandmark);
		textBoxLandmark.getElement().setAttribute("required", "true");
		textBoxLandmark.getElement().setAttribute("regEx", RegEx.ADDRESS);
		textBoxLandmark.getElement().setPropertyObject("errorBox",
				AppClientUtils.getFieldErrorBox("Please enter for landmark it will be easy to locate your hall for user"));

		Label lblContactNumber1 = new Label("* Contact Number 1");
		lblContactNumber1.setStyleName("ofhb-form-field-required-lbl");
		ftFormDemographicInformation.setWidget(5, 0, lblContactNumber1);
		lblContactNumber1.setWidth("175px");

		FlexTable ftContactNumber1 = new FlexTable();
		ftFormDemographicInformation.setWidget(5, 1, ftContactNumber1);

		textBoxContactNumber1 = new TextBox();
		textBoxContactNumber1.setMaxLength(10);
		textBoxContactNumber1.setVisibleLength(10);
		ftContactNumber1.setWidget(0, 0, textBoxContactNumber1);

		textBoxContactNumber1.getElement().setAttribute("required", "true");
		textBoxContactNumber1.getElement().setAttribute("regEx", RegEx.MOBILE_NUMBER);
		textBoxContactNumber1.getElement().setPropertyObject("errorBox",
				AppClientUtils.getFieldErrorBox("Please enter valid primary mobile phone number."));

		Label lblContactNumber2 = new Label("Contact Number 2 :");
		lblContactNumber2.setStyleName("ofhb-form-field-lbl");
		ftFormDemographicInformation.setWidget(6, 0, lblContactNumber2);
		lblContactNumber2.setWidth("175px");

		FlexTable ftContactNumber2 = new FlexTable();
		ftFormDemographicInformation.setWidget(6, 1, ftContactNumber2);

		textBoxContactNumber2 = new TextBox();
		textBoxContactNumber2.setMaxLength(10);
		textBoxContactNumber2.setVisibleLength(10);
		ftContactNumber2.setWidget(0, 0, textBoxContactNumber2);

		textBoxContactNumber2.getElement().setAttribute("required", "false");
		textBoxContactNumber2.getElement().setAttribute("regEx", RegEx.MOBILE_NUMBER);
		textBoxContactNumber2
				.getElement()
				.setPropertyObject(
						"errorBox",
						AppClientUtils
								.getFieldErrorBox("Please enter valid mobile phone number, used when primary not available"));

		Label lblContactNumber3 = new Label("Contact Number 3 :");
		lblContactNumber3.setStyleName("ofhb-form-field-lbl");
		ftFormDemographicInformation.setWidget(7, 0, lblContactNumber3);
		lblContactNumber3.setWidth("175px");

		FlexTable ftContactNumber3 = new FlexTable();
		ftFormDemographicInformation.setWidget(7, 1, ftContactNumber3);

		textBoxContactNumber3 = new TextBox();
		textBoxContactNumber3.setVisibleLength(10);
		textBoxContactNumber3.setMaxLength(10);
		ftContactNumber3.setWidget(0, 0, textBoxContactNumber3);
		
		textBoxContactNumber3.getElement().setAttribute("required", "false");
		textBoxContactNumber3.getElement().setAttribute("regEx", RegEx.MOBILE_NUMBER);
		textBoxContactNumber3
				.getElement()
				.setPropertyObject(
						"errorBox",
						AppClientUtils
								.getFieldErrorBox("Please enter valid mobile phone number, used when primary not available"));


		Label lblPrimaryEmail = new Label("* Primary Email :");
		lblPrimaryEmail.setStyleName("ofhb-form-field-required-lbl");
		ftFormDemographicInformation.setWidget(8, 0, lblPrimaryEmail);
		lblPrimaryEmail.setWidth("175px");

		FlexTable ftPrimaryEmail = new FlexTable();
		ftFormDemographicInformation.setWidget(8, 1, ftPrimaryEmail);

		textBoxPrimaryEmail = new TextBox();
		textBoxPrimaryEmail.setMaxLength(128);
		textBoxPrimaryEmail.setVisibleLength(50);
		ftPrimaryEmail.setWidget(0, 0, textBoxPrimaryEmail);

		textBoxPrimaryEmail.getElement().setAttribute("required", "true");
		textBoxPrimaryEmail.getElement().setAttribute("regEx", RegEx.EMAIL);
		textBoxPrimaryEmail
				.getElement()
				.setPropertyObject(
						"errorBox",
						AppClientUtils
								.getFieldErrorBox("Please enter valid primary email address, for inquiry purpose "));

		ftFormDemographicInformation.getCellFormatter()
				.setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		ftFormDemographicInformation.getCellFormatter()
				.setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		ftFormDemographicInformation.getCellFormatter()
				.setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		ftFormDemographicInformation.getCellFormatter()
				.setHorizontalAlignment(3, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		ftFormDemographicInformation.getCellFormatter()
				.setHorizontalAlignment(5, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		ftFormDemographicInformation.getCellFormatter()
				.setHorizontalAlignment(6, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		ftFormDemographicInformation.getCellFormatter()
				.setHorizontalAlignment(8, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		ftFormDemographicInformation.getCellFormatter()
				.setHorizontalAlignment(7, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		ftFormDemographicInformation.getCellFormatter()
				.setHorizontalAlignment(4, 0, HasHorizontalAlignment.ALIGN_RIGHT);

		VerticalPanel vpFacilityInformation = new VerticalPanel();
		vpFacilityInformation.setStyleName("ofhb-form-section");
		setWidget(4, 0, vpFacilityInformation);
		vpFacilityInformation.setWidth("100%");

		Label lblFacilityInformation = new Label("Facility Information");
		lblFacilityInformation.setStyleName("ofhb-form-section-header-lbl");
		vpFacilityInformation.add(lblFacilityInformation);

		ftFormFacilityInformation = new FlexTable();
		ftFormFacilityInformation.setCellSpacing(5);
		vpFacilityInformation.add(ftFormFacilityInformation);
		ftFormFacilityInformation.setSize("", "");

		Label lblHallType = new Label("* Hall Type :");
		lblHallType.setStyleName("ofhb-form-field-required-lbl");
		lblHallType.setWordWrap(false);
		ftFormFacilityInformation.setWidget(0, 0, lblHallType);
		lblHallType.setWidth("175px");

		FlexTable ftHallType = new FlexTable();
		ftFormFacilityInformation.setWidget(0, 1, ftHallType);

		listBoxHallType = new ListBox();
		listBoxHallType.addItem("-Select Hall type-");
		listBoxHallType.addItem("Indoor");
		listBoxHallType.addItem("Outdoor");
		listBoxHallType.addItem("Lawn");
		listBoxHallType.addItem("Gardern");
		ftHallType.setWidget(0, 0, listBoxHallType);

		ftErrorHallType = new FlexTable();
		ftErrorHallType.setStyleName("ofhb-form-field-error-table");
		ftHallType.setWidget(0, 1, ftErrorHallType);
		ftErrorHallType.setVisible(false);

		Image image_17 = new Image("resources/images/field_error.png");
		ftErrorHallType.setWidget(0, 0, image_17);
		image_17.setSize("16px", "16px");

		Label lblPleaseSelectCorrect = new Label("Please select correct Hall type");
		lblPleaseSelectCorrect.setWordWrap(false);
		ftErrorHallType.setWidget(0, 1, lblPleaseSelectCorrect);

		listBoxHallType.getElement().setPropertyObject("errorBox", ftErrorHallType);

		Label lblFunctionalHallCapacity = new Label("* Functional Hall Capacity :");
		lblFunctionalHallCapacity.setStyleName("ofhb-form-field-required-lbl");
		lblFunctionalHallCapacity.setWordWrap(false);
		ftFormFacilityInformation.setWidget(1, 0, lblFunctionalHallCapacity);
		lblFunctionalHallCapacity.setWidth("175px");

		FlexTable ftFunctionalHallCapacity = new FlexTable();
		ftFormFacilityInformation.setWidget(1, 1, ftFunctionalHallCapacity);

		textBoxFunctionalHallCapacity = new TextBox();
		textBoxFunctionalHallCapacity.setMaxLength(6);
		textBoxFunctionalHallCapacity.setVisibleLength(6);
		ftFunctionalHallCapacity.setWidget(0, 0, textBoxFunctionalHallCapacity);
		textBoxFunctionalHallCapacity.getElement().setAttribute("required", "true");
		textBoxFunctionalHallCapacity.getElement().setAttribute("regEx", RegEx.NUMERIC);
		textBoxFunctionalHallCapacity.getElement().setPropertyObject("errorBox",
				AppClientUtils.getFieldErrorBox("Please enter Main hall capacity only)"));

		ftFormFacilityInformation.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		ftFormFacilityInformation.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT);

		Label lblDiningHallCapacity = new Label("* Dining Hall Capacity :");
		lblDiningHallCapacity.setWordWrap(false);
		lblDiningHallCapacity.setStyleName("ofhb-form-field-required-lbl");
		ftFormFacilityInformation.setWidget(2, 0, lblDiningHallCapacity);
		lblDiningHallCapacity.setWidth("175px");

		
		FlexTable flexTableDiningHallCapacity = new FlexTable();
		ftFormFacilityInformation.setWidget(2, 1, flexTableDiningHallCapacity);

		textBoxDiningHallCapacity = new TextBox();
		textBoxDiningHallCapacity.setVisibleLength(6);
		textBoxDiningHallCapacity.setMaxLength(6);
		flexTableDiningHallCapacity.setWidget(0, 0, textBoxDiningHallCapacity);
		ftFormFacilityInformation.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_RIGHT);

		textBoxDiningHallCapacity.getElement().setAttribute("required", "true");
		textBoxDiningHallCapacity.getElement().setAttribute("regEx", RegEx.NUMERIC);
		textBoxDiningHallCapacity.getElement().setPropertyObject("errorBox",
				AppClientUtils.getFieldErrorBox("Please enter dining hall capacity (Numeric only)"));
		
		
		CaptionPanel cptnpnlNewPanel = new CaptionPanel("Total Rooms");
		cptnpnlNewPanel.setStyleName("ofhb-form-field-required-lbl");
		ftFormFacilityInformation.setWidget(3, 1, cptnpnlNewPanel);
		cptnpnlNewPanel.setWidth("300px");

		FlexTable flexTable = new FlexTable();
		cptnpnlNewPanel.setContentWidget(flexTable);
		flexTable.setSize("100%", "");

		Label lblAc = new Label("AC");
		lblAc.setWordWrap(false);
		lblAc.setStyleName("ofhb-form-field-required-lbl");
		flexTable.setWidget(0, 1, lblAc);
		lblAc.setWidth("");

		Label lblNonac = new Label("Non-AC");
		lblNonac.setStyleName("ofhb-form-field-required-lbl");
		flexTable.setWidget(0, 2, lblNonac);

		Label lblGeneralRooms = new Label("General Rooms");
		lblGeneralRooms.setStyleName("ofhb-form-field-lbl");
		lblGeneralRooms.setWordWrap(false);
		flexTable.setWidget(1, 0, lblGeneralRooms);
		flexTable.getCellFormatter().setWidth(1, 0, "100px");

		textBoxGeneralRoomsAC = new TextBox();
		textBoxGeneralRoomsAC.setText("0");
		textBoxGeneralRoomsAC.setVisibleLength(3);
		textBoxGeneralRoomsAC.setMaxLength(3);
		flexTable.setWidget(1, 1, textBoxGeneralRoomsAC);

		textBoxGeneralRoomsNonAC = new TextBox();
		textBoxGeneralRoomsNonAC.setText("0");
		textBoxGeneralRoomsNonAC.setVisibleLength(3);
		textBoxGeneralRoomsNonAC.setMaxLength(3);
		flexTable.setWidget(1, 2, textBoxGeneralRoomsNonAC);

		Label lblBrideRooms = new Label("Bride Rooms");
		lblBrideRooms.setStyleName("ofhb-form-field-lbl");
		lblBrideRooms.setWordWrap(false);
		flexTable.setWidget(2, 0, lblBrideRooms);

		textBoxBrideRoomsAC = new TextBox();
		textBoxBrideRoomsAC.setText("0");
		textBoxBrideRoomsAC.setVisibleLength(3);
		textBoxBrideRoomsAC.setMaxLength(3);
		flexTable.setWidget(2, 1, textBoxBrideRoomsAC);

		textBoxBrideRoomsNonAC = new TextBox();
		textBoxBrideRoomsNonAC.setText("0");
		textBoxBrideRoomsNonAC.setVisibleLength(3);
		textBoxBrideRoomsNonAC.setMaxLength(3);
		flexTable.setWidget(2, 2, textBoxBrideRoomsNonAC);

		Label lblGroomRooms = new Label("Groom Rooms");
		lblGroomRooms.setStyleName("ofhb-form-field-lbl");
		lblGroomRooms.setWordWrap(false);
		flexTable.setWidget(3, 0, lblGroomRooms);

		textBoxGroomRoomsAC = new TextBox();
		textBoxGroomRoomsAC.setText("0");
		textBoxGroomRoomsAC.setVisibleLength(3);
		textBoxGroomRoomsAC.setMaxLength(3);
		flexTable.setWidget(3, 1, textBoxGroomRoomsAC);

		textBoxGroomRoomsNonAC = new TextBox();
		textBoxGroomRoomsNonAC.setText("0");
		textBoxGroomRoomsNonAC.setVisibleLength(3);
		textBoxGroomRoomsNonAC.setMaxLength(3);
		flexTable.setWidget(3, 2, textBoxGroomRoomsNonAC);
		flexTable.getCellFormatter().setVerticalAlignment(3, 0, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setVerticalAlignment(2, 0, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.getCellFormatter().setHorizontalAlignment(3, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.getCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setHorizontalAlignment(2, 1, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setHorizontalAlignment(3, 1, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setHorizontalAlignment(2, 2, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setHorizontalAlignment(3, 2, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setVerticalAlignment(1, 1, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setVerticalAlignment(2, 1, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setVerticalAlignment(3, 1, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setVerticalAlignment(1, 2, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setVerticalAlignment(2, 2, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setVerticalAlignment(3, 2, HasVerticalAlignment.ALIGN_MIDDLE);

		CaptionPanel cptnpnlNewPanel_1 = new CaptionPanel("Parking");
		cptnpnlNewPanel_1.setStyleName("ofhb-form-field-required-lbl");
		ftFormFacilityInformation.setWidget(4, 1, cptnpnlNewPanel_1);
		cptnpnlNewPanel_1.setWidth("300px");

		FlexTable flexTable_1 = new FlexTable();
		cptnpnlNewPanel_1.setContentWidget(flexTable_1);
		flexTable_1.setSize("100%", "");

		Label lblInside = new Label("Inside");
		lblInside.setWordWrap(false);
		lblInside.setStyleName("ofhb-form-field-required-lbl");
		flexTable_1.setWidget(0, 1, lblInside);
		lblInside.setWidth("");

		Label lblOutside = new Label("Outside");
		lblOutside.setStyleName("ofhb-form-field-required-lbl");
		flexTable_1.setWidget(0, 2, lblOutside);

		Label lblTwoWheelers = new Label("Two Wheeler");
		lblTwoWheelers.setWordWrap(false);
		lblTwoWheelers.setStyleName("ofhb-form-field-lbl");
		flexTable_1.setWidget(1, 0, lblTwoWheelers);
		flexTable_1.getCellFormatter().setWidth(1, 0, "100px");

		textBoxTwoWheelersInside = new TextBox();
		textBoxTwoWheelersInside.setText("0");
		textBoxTwoWheelersInside.setVisibleLength(3);
		textBoxTwoWheelersInside.setMaxLength(3);
		flexTable_1.setWidget(1, 1, textBoxTwoWheelersInside);

		textBoxTwoWheelersOutside = new TextBox();
		textBoxTwoWheelersOutside.setText("0");
		textBoxTwoWheelersOutside.setVisibleLength(3);
		textBoxTwoWheelersOutside.setMaxLength(3);
		flexTable_1.setWidget(1, 2, textBoxTwoWheelersOutside);

		Label lblFourWheelers = new Label("Four Wheeler");
		lblFourWheelers.setWordWrap(false);
		lblFourWheelers.setStyleName("ofhb-form-field-lbl");
		flexTable_1.setWidget(2, 0, lblFourWheelers);

		textBoxFourWheelersInside = new TextBox();
		textBoxFourWheelersInside.setText("0");
		textBoxFourWheelersInside.setVisibleLength(3);
		textBoxFourWheelersInside.setMaxLength(3);
		flexTable_1.setWidget(2, 1, textBoxFourWheelersInside);

		textBoxFourWheelersOutside = new TextBox();
		textBoxFourWheelersOutside.setText("0");
		textBoxFourWheelersOutside.setVisibleLength(3);
		textBoxFourWheelersOutside.setMaxLength(3);
		flexTable_1.setWidget(2, 2, textBoxFourWheelersOutside);
		flexTable_1.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable_1.getCellFormatter().setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable_1.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable_1.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable_1.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable_1.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable_1.getCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable_1.getCellFormatter().setHorizontalAlignment(2, 1, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable_1.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable_1.getCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable_1.getCellFormatter().setHorizontalAlignment(2, 2, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable_1.getCellFormatter().setVerticalAlignment(2, 1, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable_1.getCellFormatter().setVerticalAlignment(1, 1, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable_1.getCellFormatter().setVerticalAlignment(1, 2, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable_1.getCellFormatter().setVerticalAlignment(2, 2, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable_1.getCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable_1.getCellFormatter().setVerticalAlignment(2, 0, HasVerticalAlignment.ALIGN_MIDDLE);

		chckbxAirConditionerFacility = new CheckBox("Air Conditioner Facility");
		/*chckbxAirConditionerFacility.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				boolean checked = ((CheckBox) event.getSource()).getValue();
				Window.alert("It is " + (checked ? "" : "not ") + "checked");
			}
		});*/
		ftFormFacilityInformation.setWidget(5, 1, chckbxAirConditionerFacility);

		chckbxGeneratorFacility = new CheckBox("Generator Facility");
		ftFormFacilityInformation.setWidget(6, 1, chckbxGeneratorFacility);

		Label lblDescription = new Label("Description :");
		lblDescription.setWordWrap(false);
		lblDescription.setStyleName("ofhb-form-field-lbl");
		ftFormFacilityInformation.setWidget(7, 0, lblDescription);
		lblDescription.setWidth("175px");
		ftFormFacilityInformation.getCellFormatter().setHorizontalAlignment(7, 0, HasHorizontalAlignment.ALIGN_RIGHT);

		FlexTable flexTable_2 = new FlexTable();
		ftFormFacilityInformation.setWidget(7, 1, flexTable_2);

		textAreaDesc = new TextArea();
		flexTable_2.setWidget(0, 0, textAreaDesc);
		textAreaDesc.setSize("312px", "100px");
		flexTable_2.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
		flexTable_2.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
		ftFormFacilityInformation.getCellFormatter().setVerticalAlignment(7, 0, HasVerticalAlignment.ALIGN_TOP);
		ftFormFacilityInformation.getCellFormatter().setHorizontalAlignment(6, 1, HasHorizontalAlignment.ALIGN_LEFT);
		ftFormFacilityInformation.getCellFormatter().setHorizontalAlignment(5, 1, HasHorizontalAlignment.ALIGN_LEFT);

		FlexTable ftButtonPanel = new FlexTable();
		ftButtonPanel.setCellSpacing(5);
		setWidget(5, 0, ftButtonPanel);
		getCellFormatter().setStyleName(5, 0, "ofhb-form-section-header-lbl");
		ftButtonPanel.setWidth("");

		btnSubmit = new Button();
		btnSubmit.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (isFormValid()) {
					 submit();
				}
			}
		});
		btnSubmit.setText("Submit");
		ftButtonPanel.setWidget(0, 0, btnSubmit);
		btnSubmit.setWidth("100px");

		btnReset = new Button();
		btnReset.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				reset();
			}
		});
		btnReset.setText("Reset");
		ftButtonPanel.setWidget(0, 1, btnReset);
		btnReset.setWidth("100px");
		getCellFormatter().setHorizontalAlignment(5, 0, HasHorizontalAlignment.ALIGN_CENTER);

	}

	private void submit() {
		
		HallDTO hallDTO = getHallDTO();
		AsyncCallback<HallDTO> callback = new AsyncCallback<HallDTO>() {

			@Override
			public void onSuccess(HallDTO result) {
				Window.alert("Success : " + result.getId());
				reset();
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("onFailure : " + caught.getMessage());

			}
		};
		ServiceUtil.DATA_WRITER_SERVICE.save(hallDTO, callback);
	}

	private void reset() {
		int size = getFormValidationWidgets().size();
		for (int i = size - 1; i >= 0; i--) {
			FocusWidget temp = getFormValidationWidgets().get(i);
			if (temp.getClass().toString().contains("ListBox")) {
				ListBox tempListBox = (ListBox) temp;
				tempListBox.setSelectedIndex(0);
				clearError(tempListBox);
			} else if (temp.getClass().toString().contains("TextBox")) {
				TextBox tempTextBox = (TextBox) temp;
				tempTextBox.setText("");
				clearError(tempTextBox);
			}
		}
		clearError(suggestBoxCityState);
		suggestBoxCityState.getTextBox().setText("");
	}

	private boolean isFormValid() {
		boolean result = true;

		if (isSuggestBoxValid(suggestBoxCityState)) {
			clearError(suggestBoxCityState);
		} else {
			setErrorFor(suggestBoxCityState);
			suggestBoxCityState.setFocus(true);
			result = false;
		}

		
		int size = getFormValidationWidgets().size();
		for (int i = size - 1; i >= 0; i--) {
			FocusWidget temp = getFormValidationWidgets().get(i);
			if (temp.getClass().toString().contains("ListBox")) {
				ListBox listBox = (ListBox) temp;
				if (!validate(listBox)) {
					result = false;
					setErrorFor(listBox);
					listBox.setFocus(true);
				}
			} else if (temp.getClass().toString().contains("TextBox")) {
				TextBox txtBox = (TextBox) temp;
				if (!validate(txtBox)) {
					result = false;
					setErrorFor(txtBox);
					txtBox.setFocus(true);
				}
			}
		}
		return result;
	}

	private boolean isSuggestBoxValid(SuggestBox suggestBox) {
		String str = suggestBox.getTextBox().getText().trim();
		if (str != null) {
			CityDTO cityDTO = DataCache.getCityDTO(str);
			if (cityDTO != null) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	private void setErrorFor(final TextBox txtBox) {

		txtBox.getElement().getStyle().setProperty("border", "1px solid red");
		FlexTable ftError = (FlexTable) txtBox.getElement().getPropertyObject("errorBox");
		ftError.setVisible(true);
		FlexTable parent = (FlexTable) txtBox.getParent();
		parent.setWidget(0, 1, ftError);

		txtBox.addBlurHandler(new BlurHandler() {

			@Override
			public void onBlur(BlurEvent event) {
				if (validate(txtBox)) {
					clearError(txtBox);
				} else {
					setErrorFor(txtBox);
				}
			}
		});
	}

	private void setErrorFor(final SuggestBox suggestBox) {

		suggestBox.getElement().getStyle().setProperty("border", "1px solid red");
		FlexTable ftError = (FlexTable) suggestBox.getElement().getPropertyObject("errorBox");
		ftError.setVisible(true);
		FlexTable parent = (FlexTable) suggestBox.getParent();
		parent.setWidget(0, 1, ftError);

		suggestBox.getTextBox().addBlurHandler(new BlurHandler() {

			@Override
			public void onBlur(BlurEvent event) {
				if (isSuggestBoxValid(suggestBox)) {
					clearError(suggestBox);
				} else {
					setErrorFor(suggestBox);
				}
			}
		});
	}

	private void clearError(SuggestBox suggestBox) {
		FlexTable ftError = (FlexTable) suggestBox.getElement().getPropertyObject("errorBox");
		ftError.removeFromParent();
		suggestBox.getElement().getStyle().clearBorderColor();
	}

	private void clearError(TextBox txtBox) {
		FlexTable ftError = (FlexTable) txtBox.getElement().getPropertyObject("errorBox");
		ftError.removeFromParent();
		txtBox.getElement().getStyle().clearBorderColor();
	}

	private boolean validate(TextBox txtBox) {
		String strRegEx = txtBox.getElement().getAttribute("regEx");
		String strVal = txtBox.getText().trim();
		if (txtBox.getElement().getAttribute("required").equalsIgnoreCase("false")) {
			if (strVal.trim().isEmpty())
				return true;
			else
				return checkRegEx(strVal, strRegEx);
		} else {
			return checkRequired(strVal, strRegEx);
		}
	}

	private boolean checkRequired(String strVal, String strRegEx) {
		if (strVal.trim().isEmpty()) {
			return false;
		} else {
			return checkRegEx(strVal, strRegEx);
		}
	}

	private boolean checkRegEx(String strVal, String strRegEx) {
		return strVal.matches(strRegEx);
	}

	private boolean validate(ListBox listBox) {

		if (listBox.getSelectedIndex() > 0) {
			return true;
		}

		return false;
	}

	private void setErrorFor(final ListBox listBox) {

		listBox.setStyleName("gwt-ListBox-error");
		FlexTable ftError = (FlexTable) listBox.getElement().getPropertyObject("errorBox");
		ftError.setVisible(true);
		listBox.addBlurHandler(new BlurHandler() {

			@Override
			public void onBlur(BlurEvent event) {
				if (validate(listBox)) {
					clearError(listBox);
				} else {
					setErrorFor(listBox);
				}
			}
		});

		listBox.addFocusHandler(new FocusHandler() {

			@Override
			public void onFocus(FocusEvent event) {
				if (validate(listBox)) {
					clearError(listBox);
				} else {
					setErrorFor(listBox);
				}
			}
		});
	}

	private void clearError(ListBox listBox) {
		FlexTable ftError = (FlexTable) listBox.getElement().getPropertyObject("errorBox");
		ftError.setVisible(false);
		listBox.setStyleName("gwt-ListBox");
	}

	public ArrayList<FocusWidget> getFormValidationWidgets() {

		if (formValidationWidgets == null) {
			formValidationWidgets = new ArrayList<FocusWidget>();

			formValidationWidgets.add(comboBoxOwnerOfHall);
			formValidationWidgets.add(textBoxHallFullName);
			formValidationWidgets.add(textBoxShortName);

			formValidationWidgets.add(textBoxAddressLine1);
			formValidationWidgets.add(textBoxAddressLine2);
			formValidationWidgets.add(textBoxPinCode);
			

			formValidationWidgets.add(textBoxContactNumber1);
			formValidationWidgets.add(textBoxContactNumber2);
			formValidationWidgets.add(textBoxContactNumber3);
			
			formValidationWidgets.add(textBoxPrimaryEmail);

			formValidationWidgets.add(listBoxHallType);
			formValidationWidgets.add(textBoxFunctionalHallCapacity);
			formValidationWidgets.add(textBoxDiningHallCapacity);
		}

		return formValidationWidgets;
	}

	private HallDTO getHallDTO() {
		HallDTO hallDTO = new HallDTO();
		
		UserDTO userDTO = mapUserDTO.get(comboBoxOwnerOfHall.getValue(comboBoxOwnerOfHall.getSelectedIndex()));
		hallDTO.setUserDTO(userDTO);
		
		hallDTO.setFullName(textBoxHallFullName.getText().trim());
		hallDTO.setShortName(textBoxShortName.getText().trim());
			
		hallDTO.setAddressLine1(textBoxAddressLine1.getText().trim());
		hallDTO.setAddressLine2(textBoxAddressLine2.getText().trim());
		
		hallDTO.setCityDTO(DataCache.getCityDTO(suggestBoxCityState.getTextBox().getText().trim()));
		
		hallDTO.setPinCode(Long.parseLong(textBoxPinCode.getText().trim()));
		hallDTO.setLandmark(textBoxLandmark.getText().trim());
		hallDTO.setContactNumber1(Long.parseLong(textBoxContactNumber1.getText().trim()));
		
		if(textBoxContactNumber2.getText().trim().length() >0)
			hallDTO.setContactNumber2(Long.parseLong(textBoxContactNumber2.getText().trim()));
		
		if(textBoxContactNumber3.getText().trim().length() >0)
			hallDTO.setContactNumber3(Long.parseLong(textBoxContactNumber3.getText().trim()));
					
		hallDTO.setPrimaryEmail(textBoxPrimaryEmail.getText().trim());
				
		hallDTO.setHallType(listBoxHallType.getItemText(listBoxHallType.getSelectedIndex()));
		hallDTO.setCapacityMainHall(Integer.parseInt(textBoxFunctionalHallCapacity.getText().trim()));
		hallDTO.setCapacityDiningHall(Integer.parseInt(textBoxDiningHallCapacity.getText().trim()));
		hallDTO.setGeneralRoomsAc(Integer.parseInt(textBoxGeneralRoomsAC.getText().trim()));
		hallDTO.setGeneralRoomsNonac(Integer.parseInt(textBoxGeneralRoomsNonAC.getText().trim()));
		hallDTO.setBrideRoomsAc(Integer.parseInt(textBoxBrideRoomsAC.getText().trim()));
		hallDTO.setBrideRoomsNonac(Integer.parseInt(textBoxBrideRoomsNonAC.getText().trim()));
		hallDTO.setGroomRoomsAc(Integer.parseInt(textBoxGeneralRoomsAC.getText().trim()));
		hallDTO.setGroomRoomsNonac(Integer.parseInt(textBoxGeneralRoomsNonAC.getText().trim()));
			
		hallDTO.setParkingInsideTwoWheeler(Integer.parseInt(textBoxTwoWheelersInside.getText().trim()));
		hallDTO.setParkingOutsideTwoWheeler(Integer.parseInt(textBoxTwoWheelersOutside.getText().trim()));
		hallDTO.setParkingInsideFourWheeler(Integer.parseInt(textBoxFourWheelersInside.getText().trim()));
		hallDTO.setParkingOutsideFourWheeler(Integer.parseInt(textBoxFourWheelersOutside.getText().trim()));
			
		hallDTO.setFacGenerator(chckbxGeneratorFacility.getValue());
		hallDTO.setFacAcHall(chckbxAirConditionerFacility.getValue());
			
		hallDTO.setDescription(textAreaDesc.getText().trim());

		return hallDTO;
	}

	
}
